package com.puppet.building182.service.impl;

import com.puppet.building182.handler.MyAssert;
import com.puppet.building182.mapper.HouseMapper;
import com.puppet.building182.pojo.DTO.HouseDto;
import com.puppet.building182.pojo.House;
import com.puppet.building182.pojo.VO.HouseState;
import com.puppet.building182.pojo.VO.HouseVo;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.HouseService;
import com.puppet.building182.service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {
@Autowired
HouseMapper houseMapper;
@Autowired
    OwnerService ownerService;
    @Override
    public Integer countHouseByBuildingId(Integer id) {
        return houseMapper.countHouseByBuildingId(id);

    }

    @Override
    public List<HouseVo> getHouseIdAnd() {
        return houseMapper.getHouseIdAnd();
    }

    @Override
    public Integer addHouse(House house) {
        Integer result = houseMapper.addHouse(house);
        MyAssert.error( result <= 0, BuildingCode.SAVE_FAIL.getMessage());
        return result;

    }

    @Override
    public Integer deleteById(Integer id) {
        Integer num = ownerService.selectOwner(id);
        MyAssert.error(num > 0, BuildingCode.DELETE_BUILD_FAIL.getMessage());
        return houseMapper.deleteById(id);

    }

    @Override
    public Integer deleteHouse(List<Integer> house) {
       for(Integer id : house) {
           Integer num = ownerService.selectOwner(id);
           MyAssert.error(num > 0, BuildingCode.DELETE_BUILD_FAIL.getMessage());
       }
        return houseMapper.deleteHouse(house);

    }

    @Override
    public List<House> getHouse(HouseDto houseDto) {
        return houseMapper.getHouse(houseDto);
    }

    @Override
    public House getHouseById(Integer id) {
        return houseMapper.getHouseById(id);
    }

    @Override
    public List<HouseState> getState() {
        return houseMapper.getState();
    }

    @Override
    public Integer updateHouse(House house) {
        Integer result = houseMapper.updateHouse(house);
        MyAssert.error( result <= 0, BuildingCode.SAVE_FAIL.getMessage());
        return result;
    }
}
